﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eVoteClient.util;
using System.Security.Cryptography;

namespace eVoteClient
{
    public partial class ViewClosedPolls : Form
    {
        int pollID;
        Boolean published = false;
        string[] answers;
        string[] values;

        public ViewClosedPolls(int pollID)
        {
            InitializeComponent();
            this.pollID = pollID;
            hashBox.Enabled = false;
            isAdmin();
            isPublished();
            init();
        }

        private void init()
        {
            var socket = global.socket;

            socket.writer.WriteLine("POLL_REQ#" + pollID);
            socket.writer.Flush();
            string reply = socket.reader.ReadLine();
            string question = reply.Split('#')[5];

            questionLabel.Text = question;
            answers = new string[reply.Split('#').Length - 6];
            for (int i = 6, j = 0; i < reply.Split('#').Length; i++, j++)
                answers[j] = reply.Split('#')[i];

            socket.writer.WriteLine("RES_REQ#" + pollID);
            socket.writer.Flush();
            reply = socket.reader.ReadLine();
            values = new string[reply.Split('#').Length - 1];
            for (int i = 1, j = 0; i < reply.Split('#').Length; i++, j++)
                values[j] = reply.Split('#')[i];

            if (values[0].Equals("0"))
            {
                published = false;
                isPublished();
            }
            else
            {
                published = true;
                isPublished();
            }

            socket.writer.WriteLine("CHK_VOT#" + pollID);
            socket.writer.Flush();
            reply = socket.reader.ReadLine();
            string encVote = reply.Split('#')[1];
            hashBox.Text = encVote;
        }
        private void publishButton_Click(object sender, EventArgs e)
        {
            var socket = global.socket;

            socket.writer.WriteLine("DCR_POL#" + pollID + '#' + publishTextBox.Text);
            socket.writer.Flush();

            string reply = socket.reader.ReadLine();

            isAdmin();
            isPublished();
            init();
        }

        private void isAdmin()
        {
            if (global.isAdmin)
            {
                answerLabel.Visible = false;
                hashBox.Visible = false;
            }
            else
            {
                answerLabel.Visible = true;
                hashBox.Visible = true;
            }
        }

        private void isPublished()
        {
            var socket = global.socket;
            socket.writer.WriteLine("RES_REQ#" + pollID);
            socket.writer.Flush();

            string reply = socket.reader.ReadLine();
            string[] values = new string[reply.Split('#').Length - 1];
            for (int i = 1, j = 0; i < reply.Split('#').Length; i++, j++)
                values[j] = reply.Split('#')[i];

            if (values.Length<=1)
                published = false;
            else
                published = true;

            if (published == false)
            {
                viewResultsButton.Visible = false;
                if (global.isAdmin)
                {
                    publishButton.Visible = true;
                    publishTextBox.Visible = true;
                    notPublishedLabel.Visible = true;
                }
                else
                {
                    publishButton.Visible = false;
                    publishTextBox.Visible = false;
                    notPublishedLabel.Visible = true;
                }
            }
            else
            {
                viewResultsButton.Visible = true;
                publishButton.Visible = false;
                publishTextBox.Visible = false;
                notPublishedLabel.Visible = false;
            }
        }

        private void viewResultsButton_Click(object sender, EventArgs e)
        {
            ViewResults vResults = new ViewResults(answers,values);
            vResults.ShowDialog(this);
        }

        private void ViewClosedPolls_Load(object sender, EventArgs e)
        {
            this.Location = new Point(350, 200);
            this.Size = new Size(572, 324);
        }
    }
}
